package org.jeecg.modules.parameter.biz.controlltr;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.parameter.api.dto.line.SimAddLineDto;
import org.jeecg.modules.parameter.api.dto.line.SimLineDto;
import org.jeecg.modules.parameter.api.entity.SimLine;
import org.jeecg.modules.parameter.api.vo.line.SimLinePreVo;
import org.jeecg.modules.parameter.biz.service.SimLineService;
import org.jeecg.modules.parameter.common.api.dto.PatchPO;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lsy
 * @date 2024/09/17
 */
@Api(tags = "参数管理-线路管理")
@RestController
@RequestMapping(value = {"/lineInfo"}, produces = {"application/json; charset=utf-8"})
@Slf4j
public class SimLineController extends JeecgController<SimLine, SimLineService> {
    @Resource
    private SimLineService lineService;


    @ApiOperation(value = "线路管理-查询所有线路", notes = "线路管理-查询所有线路")
    @PostMapping(value = "/getAllLine")
    public Result<List<SimLine>> getAllLineInfo() {
        List<SimLine> simLine = lineService.getAllLine();
        simLine.forEach(simLine1 -> {
            if (simLine1.getLinePosition() != null) {
                String temp = simLine1.getLinePosition().replace("=", ":");
                simLine1.setLinePosition(temp);
            }
        });

        return Result.ok(simLine);

    }

    @ApiOperation(value = "线路管理-添加线路", notes = "线路管理-添加线路")
    @PostMapping(value = "/addLine")
    public Result<String> saveLine(@RequestBody SimAddLineDto simAddLineDto) {
        SimLine simLineEntity = oConvertUtils.entityToModel(simAddLineDto, SimLine.class);
        String resultUuid = lineService.saveLineParameter(simLineEntity);
        return Result.ok(resultUuid);
    }

    @ApiOperation(value = "线路管理-删除线路", notes = "线路管理-删除线路")
    @DeleteMapping(value = "/deleteLine")
    public Result<String> deleteLine(@RequestParam(name = "id", required = true) String id) {
        lineService.removeById(id);
        return Result.ok("删除成功");
    }



    @ApiOperation(value = "线路管理-修改", notes = "线路管理-修改")
    @PutMapping(value = "/editLine")
    public Result<Boolean> updateLine(@RequestBody PatchPO patchPo) {
        return Result.ok(lineService.updatePatch(patchPo));
    }

    @ApiOperation(value = "线路管理-校验线路ID唯一性", notes = "线路管理-校验线路ID唯一性")
    @PostMapping(value = "/checkLineId")
    public Result<Boolean> checkLineId(@NotNull @RequestParam(name = "lineUuid") String lineUuid, @RequestParam(name = "lineId") String lineId) {
        return Result.ok(lineService.checkLineId(lineUuid, lineId));
    }

    @ApiOperation(value = "线路管理-更新排序", notes = "线路管理-更新排序")
    @PostMapping(value = "/updateSortNo")
    public Result<Boolean> updateSortNo(@RequestBody List<SimLineDto> sortLines) {
        List<SimLine> simLineEntity = oConvertUtils.entityListToModelList(sortLines, SimLine.class);
        return Result.ok(lineService.updateSortNo(simLineEntity));
    }


    @ApiOperation(value = "线路管理-线路信息预览", notes = "线路管理-线路信息预览")
    @PostMapping(value = "/selectPre")
    public Result<List<SimLinePreVo>> selectPre(@RequestParam(name = "lineId") String lineId) {
        List<SimLinePreVo> simLinePre = oConvertUtils.entityListToModelList(lineService.selectPre(lineId), SimLinePreVo.class);
        return Result.ok(simLinePre);
    }
}
